<%@ page import="java.util.Objects" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.ResultSet" %><%--
  Created by IntelliJ IDEA.
  User: lenovo
  Date: 2024-05-08
  Time: 16:07
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String id = request.getParameter("id");
    Object username = session.getAttribute("username");
    if(Objects.isNull(id)){
        out.println("<script type=\"text/javascript\">");
        out.println("alert('无ID值，出错啦');");
        out.println("window.location.href='MessageShow.jsp';");
        out.println("</script>");
        return;
    } else if(Objects.isNull(username)) {
        out.println("<script type=\"text/javascript\">");
        out.println("alert('请先登录');");
        out.println("window.location.href='MessageShow.jsp';");
        out.println("</script>");
        return;
    } else {
        // 注册驱动
        Class.forName("com.mysql.jdbc.Driver");
//        获取链接
//        useServerPrepStmts=true开启预编译功能
        String url = "jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&useServerPrepStmts=true";
        String sqlUsername = "root";
        String sqlPassword = "123456";
        Connection conn = DriverManager.getConnection(url, sqlUsername, sqlPassword);
        String sql = "select * from user where username = ? ";
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        preparedStatement.setString(1, username.toString());
        ResultSet resultSetQuery = preparedStatement.executeQuery();
        if (!resultSetQuery.next()) {
            conn.close();
            preparedStatement.close();
            out.println("<script type=\"text/javascript\">");
            out.println("alert('请先登录');");
            out.println("window.location.href='MessageShow.jsp';");
            out.println("</script>");
            return;
        }  else if(resultSetQuery.getInt("flag") == 1){
            conn.close();
            preparedStatement.close();
            out.println("<script type=\"text/javascript\">");
            out.println("alert('无权限');");
            out.println("window.location.href='MessageShow.jsp';");
            out.println("</script>");
            return;
        }

//        执行语句
        sql = "delete from message where id = ? ";
//        获取执行对象
        preparedStatement = conn.prepareStatement(sql);
        preparedStatement.setString(1, id);
//        返回结果
        int resultSet = preparedStatement.executeUpdate();
        if (resultSet <= 0) {
            conn.close();
            preparedStatement.close();
            out.println("<script type=\"text/javascript\">");
            out.println("alert('删除出错');");
            out.println("window.location.href='MessageShow.jsp';");
            out.println("</script>");
        }
        conn.close();
        preparedStatement.close();
        out.println("<script type=\"text/javascript\">");
        out.println("alert('删除成功');");
        out.println("window.location.href='MessageShow.jsp';");
        out.println("</script>");
    }
%>
<html>
<head>
    <title>Title</title>
</head>
<body>

</body>
</html>
